<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />

    

    

    <title>mybatis-plus集成oracle | 郑礼德技术栈</title>
    <meta name="author" content="郑礼德" />
    <meta name="keywords" content="" />
    <meta name="description" content="mybatiesPlus 集成oraclepom文件 &amp;lt;dependencies&amp;gt;        &amp;lt;dependency&amp;gt;            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;            &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;        &amp;lt;/dependency&amp;gt;..." />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />

    
    <link rel="alternate" href="/homepage/atom.xml" title="郑礼德技术栈" type="application/atom+xml">
    
    
    <link rel="icon" href="/homepage/images/favicon.ico">
    

    <style type="text/css">
    @font-face {
        font-family: 'icomoon';
        src: url("/homepage/fonts/icomoon.eot?q628ml");
        src: url("/homepage/fonts/icomoon.eot?q628ml#iefix") format('embedded-opentype'),
             url("/homepage/fonts/icomoon.ttf?q628ml") format('truetype'),
             url("/homepage/fonts/icomoon.woff?q628ml") format('woff'),
             url("/homepage/fonts/icomoon.svg?q628ml#icomoon") format('svg');
        font-weight: normal;
        font-style: normal;
    }
    </style>
    
<link rel="stylesheet" href="/homepage/css/style.css">


    <!--[if lt IE 9]>
    <style type="text/css">
    .nav-inner {top:0;}
    .author-meta {position:static;top:0;}
    .search-form {height:36px;}
    </style>
    <script type="text/javascript" src="https://unpkg.com/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
    <![endif]-->
<meta name="generator" content="Hexo 4.2.0"></head>
<body>

    <main class="app">
        <header id="header" class="header clearfix">
    <div id="nav" class="nav">
    <div class="nav-mobile">
        <button id="open-panel" class="open-panel nav-mobile-item"><i class="icon-documents"></i></button>
        <h1 class="nav-mobile-title nav-mobile-item">郑礼德技术栈</h1>
        <button id="open-menus" class="open-panel nav-mobile-item"><i class="icon-library"></i></button>
    </div>

    <nav id="nav-inner" class="nav-inner">
        
            <a class="nav-item" href="/homepage/">
                <span class="nav-text">首页</span>
            </a>
        
            <a class="nav-item" href="/homepage/categories/front-end">
                <span class="nav-text">前端</span>
            </a>
        
            <a class="nav-item" href="/homepage/categories/back-end">
                <span class="nav-text">后端</span>
            </a>
        
            <a class="nav-item" href="/homepage/categories/linux">
                <span class="nav-text">linux部署</span>
            </a>
        
            <a class="nav-item" href="/homepage/tags">
                <span class="nav-text">标签</span>
            </a>
        
            <a class="nav-item" href="/homepage/archives">
                <span class="nav-text">归档</span>
            </a>
        
            <a class="nav-item" href="/homepage/about">
                <span class="nav-text">关于</span>
            </a>
        
    </nav>
</div>

    <aside id="aside" class="aside">
    <div id="aside-mask" class="aside-mask"></div>
    <div id="aside-inner" class="aside-inner">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit"><i class="icon-search-stroke"></i></button><input type="hidden" name="sitesearch" value="http://zhenglide.gitee.io/homepage"></form>

        
        
        
        

        
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#pom文件"><span class="toc-number">1.</span> <span class="toc-text">pom文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#下载oracle-jar包并安装到本地"><span class="toc-number">2.</span> <span class="toc-text">下载oracle jar包并安装到本地</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#创建表"><span class="toc-number">3.</span> <span class="toc-text">创建表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#编写代码"><span class="toc-number">4.</span> <span class="toc-text">编写代码</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#配置类"><span class="toc-number">4.1.</span> <span class="toc-text">配置类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mapper类"><span class="toc-number">4.2.</span> <span class="toc-text">Mapper类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#实体类"><span class="toc-number">4.3.</span> <span class="toc-text">实体类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Service类"><span class="toc-number">4.4.</span> <span class="toc-text">Service类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Service实现类"><span class="toc-number">4.5.</span> <span class="toc-text">Service实现类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Controller类"><span class="toc-number">4.6.</span> <span class="toc-text">Controller类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#oracle-自增序列配置"><span class="toc-number">5.</span> <span class="toc-text">oracle 自增序列配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#最后记录一个今天碰到的问题"><span class="toc-number">6.</span> <span class="toc-text">最后记录一个今天碰到的问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Oracle-获取自增主键的方式："><span class="toc-number">6.1.</span> <span class="toc-text">Oracle 获取自增主键的方式：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Mysql-获取自增主键的方式"><span class="toc-number">6.2.</span> <span class="toc-text">Mysql 获取自增主键的方式</span></a></li></ol></li></ol>
        
    </div>
</aside>

</header>

        <div id="content" class="content">
            <div id="wrapper" class="wrapper" style="max-width: 800px">
                <article class="article" itemscope itemprop="blogPost">
    
    <header class="article-header">
        
        <h1 itemprop="name">
            mybatis-plus集成oracle
        </h1>
        
        <div class="article-meta clearfix">
            <a class="article-date" href="http://zhenglide.gitee.io/homepage/2020/06/05/mybatis-plus%E9%9B%86%E6%88%90oracle/index.html">
    
    <i class="icon-calendar"></i>
    
    <time datetime="2020-06-05T07:11:09.000Z" itemprop="datePublished">2020-06-05</time>
</a>

            

        </div>
    </header>
    
    <section class="article-body markdown-body">
        
        <p>mybatiesPlus 集成oracle</p>
<a id="more"></a>

<h2 id="pom文件"><a href="#pom文件" class="headerlink" title="pom文件"></a>pom文件</h2><pre><code class="xml"> &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;!--mybaties-plus 3.x--&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.baomidou&lt;/groupId&gt;
            &lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt;
            &lt;version&gt;3.1.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!--数据库驱动--&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.oracle&lt;/groupId&gt;
            &lt;artifactId&gt;ojdbc6&lt;/artifactId&gt;
            &lt;version&gt;11.2.0.3&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!--打印mysql 查询日志 用低版本有问题--&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;p6spy&lt;/groupId&gt;
            &lt;artifactId&gt;p6spy&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!--代码生成器--&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.baomidou&lt;/groupId&gt;
            &lt;artifactId&gt;mybatis-plus-generator&lt;/artifactId&gt;
            &lt;version&gt;3.1.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.freemarker&lt;/groupId&gt;
            &lt;artifactId&gt;freemarker&lt;/artifactId&gt;
            &lt;version&gt;2.3.30&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;!--Swagger-UI API文档生产工具--&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;io.springfox&lt;/groupId&gt;
            &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
            &lt;version&gt;2.7.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;io.springfox&lt;/groupId&gt;
            &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
            &lt;version&gt;2.7.0&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;optional&gt;true&lt;/optional&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
            &lt;optional&gt;true&lt;/optional&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                    &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;</code></pre>
<h2 id="下载oracle-jar包并安装到本地"><a href="#下载oracle-jar包并安装到本地" class="headerlink" title="下载oracle jar包并安装到本地"></a>下载oracle jar包并安装到本地</h2><p><a href="https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html." target="_blank" rel="noopener">下载地址</a></p>
<p>使用以下命令安装到本地,因为仓库没有</p>
<pre><code class="bash"> mvn install:install-file -Dfile=E:/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar  </code></pre>
<h2 id="创建表"><a href="#创建表" class="headerlink" title="创建表"></a>创建表</h2><pre><code class="sql">create table emp(
   emp_id int,
   emp_name varchar(100),
   emp_dep varchar(100)
 )
 insert into emp(emp_id,emp_name,emp_dep) values (1002,&#39;猪宝宝&#39;,&#39;研发部&#39;);</code></pre>
<h2 id="编写代码"><a href="#编写代码" class="headerlink" title="编写代码"></a>编写代码</h2><h3 id="配置类"><a href="#配置类" class="headerlink" title="配置类"></a>配置类</h3><pre><code class="java">@MapperScan(&quot;com.zhenglide.mapper&quot;)
@Configuration
public class MybatiesPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
       return  new PaginationInterceptor();


   }
    /**
     * 序列生成器
     */
//    @Bean
//    public OracleKeyGenerator oracleKeyGenerator(){
//        return new OracleKeyGenerator();
//    }
}</code></pre>
<h3 id="Mapper类"><a href="#Mapper类" class="headerlink" title="Mapper类"></a>Mapper类</h3><pre><code class="java">public interface EmpMapper extends BaseMapper&lt;Emp&gt; {
}
</code></pre>
<h3 id="实体类"><a href="#实体类" class="headerlink" title="实体类"></a>实体类</h3><pre><code class="java">
@TableName(&quot;emp&quot;)
@Data
/*@KeySequence(value=&quot;emp_seq&quot;,clazz=Integer.class)*/
@KeySequence(value=&quot;myOracleKeyGenerator&quot;,clazz=Integer.class)
public class Emp {
    @TableId(value = &quot;emp_id&quot; ,type= IdType.ID_WORKER)
    Integer empId;
    @TableField(&quot;emp_name&quot;)
    String empName;
    @TableField(&quot;emp_dep&quot;)
    String empDep;
}</code></pre>
<h3 id="Service类"><a href="#Service类" class="headerlink" title="Service类"></a>Service类</h3><pre><code class="java">public interface IEmpService extends IService&lt;Emp&gt; {
}
</code></pre>
<h3 id="Service实现类"><a href="#Service实现类" class="headerlink" title="Service实现类"></a>Service实现类</h3><pre><code class="java">@Service
public class EmpService extends ServiceImpl&lt;EmpMapper, Emp&gt; implements IEmpService {
}
</code></pre>
<h3 id="Controller类"><a href="#Controller类" class="headerlink" title="Controller类"></a>Controller类</h3><pre><code class="java">@RequestMapping(&quot;emp&quot;)
@RestController
public class EmpController {
    @Autowired
    IEmpService empService;
    @GetMapping(&quot;list&quot;)
    public List&lt;Emp&gt; list(){
         return empService.list();
    }

    @PostMapping(&quot;add&quot;)
    public  Emp add (@RequestBody Emp emp){
        empService.save(emp);
          return emp;
    }
    @DeleteMapping(&quot;delete/{id}&quot;)
    public boolean delete(@PathVariable(&quot;id&quot;) Integer id){
        return  empService.removeById(id);
    }
    @PostMapping(&quot;modify&quot;)
    public  boolean modify (@RequestBody Emp emp){
        return  empService.updateById(emp);
    }
}</code></pre>
<h2 id="oracle-自增序列配置"><a href="#oracle-自增序列配置" class="headerlink" title="oracle 自增序列配置"></a>oracle 自增序列配置</h2><p> 配置序列有两种方式，使用mybatis-plus 提供的序列生成器</p>
<pre><code class="java">    @Bean
    public OracleKeyGenerator oracleKeyGenerator(){
        return new OracleKeyGenerator();
   }</code></pre>
<p>然后在实体类中加入以下注解即可</p>
<pre><code class="java">@KeySequence(value=&quot;emp_seq&quot;,clazz=Integer.class)</code></pre>
<p>第二种方式使用自定义序列配置</p>
<pre><code class="java">@Component(&quot;myOracleKeyGenerator&quot;)
public class MyOracleKeyGenerator implements IKeyGenerator {
    @Override
    public String executeSql(String incrementerName) {
        return &quot;select emp_seq.nextval from  dual&quot;;
    }
}</code></pre>
<pre><code>@KeySequence(value=&quot;myOracleKeyGenerator&quot;,clazz=Integer.class)</code></pre><h2 id="最后记录一个今天碰到的问题"><a href="#最后记录一个今天碰到的问题" class="headerlink" title="最后记录一个今天碰到的问题"></a>最后记录一个今天碰到的问题</h2><p>使用@Insert注解的 获取插入主键的id</p>
<pre><code class="java">@Insert(&quot;insert into wx_kbt_test.IM_WXMSG_MODELNAME(ID,name,code,templateid,type,THIRDINVOKE,CHANNELMARK,&quot; +
            &quot;TEMPLETBASE,MODELPUSHNUM,MODELPUSHTIME,DEMANDDEPORPERSON,GROUP_ID,INDIVIDUATION,MODELLIMIT，ISGROUP,CREATETIME,UPDATETIME) &quot; +
            &quot;values (#{modelName.id},#{modelName.name},&quot; +
            &quot;#{modelName.code},#{modelName.templateId},#{modelName.type},#{modelName.thirdInvoke},&quot; +
            &quot;#{modelName.chanelMark},#{modelName.templateBase},#{modelName.modelPushNum},#{modelName.modelPushTime},#{modelName.demandDepOrPerson}&quot; +
            &quot;,#{modelName.groupId},#{modelName.individuation},#{modelName.modelLimit},#{modelName.isGroup},#{modelName.createTime},#{modelName.updateTime})&quot;)
    @SelectKey(keyColumn = &quot;id&quot;,resultType =Long.class,before = true,statement = &quot;SELECT SEQ_IM_XMT_TEXT_MESSAGE.NEXTVAL as ID from DUAL&quot;,keyProperty = &quot;modelName.id&quot;)
    public Long addModelName(@Param(&quot;modelName&quot;) ModelName modelName);</code></pre>
<p>@selectKey 主要keyProperty 要跟实体类对应，然后从实体类中获取新增id</p>
<h3 id="Oracle-获取自增主键的方式："><a href="#Oracle-获取自增主键的方式：" class="headerlink" title="Oracle 获取自增主键的方式："></a>Oracle 获取自增主键的方式：</h3><pre><code class="xml">&lt;insert id=&quot;insert&quot; parameterType=&quot;com.test.User&quot;&gt;
  &lt;selectKey resultType=&quot;INTEGER&quot; order=&quot;BEFORE&quot; keyProperty=&quot;userId&quot;&gt; 
    SELECT SEQ_USER.NEXTVAL as userId from DUAL
  &lt;/selectKey&gt; 
  insert into user (user_id, user_name, modified, state)
  values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{modified,jdbcType=TIMESTAMP}, #{state,jdbcType=INTEGER})
&lt;/insert&gt;</code></pre>
<h3 id="Mysql-获取自增主键的方式"><a href="#Mysql-获取自增主键的方式" class="headerlink" title="Mysql 获取自增主键的方式"></a>Mysql 获取自增主键的方式</h3><pre><code class="xml">&lt;insert id=&quot;insert&quot; parameterType=&quot;com.test.User&quot; keyProperty=&quot;userId&quot; useGeneratedKeys=&quot;true&quot; &gt;</code></pre>

        
    </section>
</article>



<a id="pagenext" href="/homepage/2020/06/04/Dockerfile%E6%9E%84%E5%BB%BAspringboot%E9%95%9C%E5%83%8F/" class="article-next" title="Dockerfile构建springboot镜像及docker插件使用"><i class="icon-arrow-right"></i></a>


<a id="pageprev" href="/homepage/2020/06/10/docker%E4%BD%BF%E7%94%A8Let-sEncrypt%E9%85%8D%E7%BD%AEhttps/" class="article-prev" title="docker使用Let&#39;sEncrypt配置https"><i class="icon-arrow-left"></i></a>




            </div>
        </div>
        <footer class="footer">
    Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>, Theme by <a href="https://github.com/sanonz/hexo-theme-concise" target="_blank">Concise</a>

    
    <script>
        var _hmt = _hmt || [];
        (function () {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?b13bb4c2ef8300062aab9732bd22eae0";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>
    
</footer>

    </main>

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.9.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
        var nodes = {
            nav: $('#nav'),
            aside: $('#aside'),
            asideInner: $('#aside-inner'),
            navInner: $('#nav-inner')
        };

        var doing = false;
        nodes.asideInner.on('webkitAnimationEnd mozAnimationEnd oAnimationEnd oanimationend animationend', function() {
            if (nodes.aside.hasClass('mobile-open')) {
                nodes.aside.removeClass('mobile-open');
            } else {
                nodes.aside.removeClass('mobile-close panel-show');
            }
            doing = false;
        });
        $('#open-panel, #aside-mask').on('click', function() {
            if (doing) {
                return;
            }

            if (nodes.aside.hasClass('panel-show')) {
                nodes.aside.addClass('mobile-close');
            } else {
                nodes.aside.addClass('mobile-open panel-show');
            }
        });
        $('#open-menus').on('click', function() {
            nodes.navInner.slideToggle();
        });

        if (window.innerWidth <= 960) {
            setTimeout(function() {
                nodes.navInner.slideUp();
            }, 3000);
        }
    });
    </script>
    
        
<script src="/homepage/js/scrollspy.min.js"></script>

        <script type="text/javascript">
        $(document.body).scrollspy({target: '#aside-inner'});

        $(window).on('resize', function() {
            var hw = $('#header').width();
            var ww = $('#wrapper').width();
            var space = ($(this).width() - hw - ww) / 2 / 2;

            var pageprev = $('#pageprev');
            var pagenext = $('#pagenext');
            var avg = (pageprev.width() + pagenext.width()) / 2

            if(space > avg) {
                var len = space - avg / 2;
                var styles = {position: 'fixed', top: '50%', marginTop: - (pageprev.width() + pagenext.width()) / 4}
                pageprev.css($.extend({left: hw + len}, styles));
                pagenext.css($.extend({right: len}, styles));
            } else {
                pageprev.removeAttr('style');
                pagenext.removeAttr('style');
            }
        }).trigger('resize');
        </script>
    

</body>
</html>
